package com.example.birdfriends.controller;

import com.example.birdfriends.pojo.post.Post;
import com.example.birdfriends.pojo.post.PostFront;
import com.example.birdfriends.pojo.post.PostsAndAuthors;
import com.example.birdfriends.pojo.search.SearchContent;
import com.example.birdfriends.pojo.search.SearchResult;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.service.PostService;
import com.example.birdfriends.service.UserService;
import com.example.birdfriends.utils.convert.ConvertService;
import com.example.birdfriends.utils.result.Result;
import com.example.birdfriends.utils.search.SearchUtil;
import com.example.birdfriends.utils.wordSplice.CutWord;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private UserService userService = null;
    @Autowired
    private PostService postService = null;
    @Autowired
    private ConvertService convertService = null;
    @Autowired
    private Result result = null;
    @Autowired
    private CutWord cutWord = null;
    @Autowired
    private SearchUtil searchUtil;

    protected ConcurrentHashMap<String, List<PostFront>> searchPostMap = new ConcurrentHashMap<>();  // 搜索到的帖子数据

    @PostMapping("/searchInfo")
    public Map<String, Object> searchInMain(@RequestBody SearchContent searchContent) throws IOException {
        String searchInfo = searchContent.getSearchInfo();
        Long curLoginUid = searchContent.getCurLoginUid();
        String category = searchContent.getCategory();
        int curPageNumber = searchContent.getCurPage();

//        System.out.println("搜索内容："+searchInfo+" "+curLoginUid);
        if(searchInfo == null || searchInfo.equals("")){
            return result.resultMap(false, "搜索内容为空，请输入搜索内容", null);
        }

        List<String> words = cutWord.cutSentence(searchInfo);
        System.out.println(Arrays.toString(words.toArray()));
        int lineNumber = 50;
        int start = (curPageNumber-1) * lineNumber;
        if (category.equals("帖子")){
            System.out.println("搜索帖子");
            List<PostFront> postList = searchPostMap.get(searchInfo);  // 检查是否已经有了搜索结果
            if (postList == null){
                // 如果还没有对应的搜索项的搜索结果
                postList = searchUtil.searchPost(words, curLoginUid, "search");
                searchPostMap.put(searchInfo, postList);  // 将从数据库查询到的结果放入map
            }
            boolean fail = postList.isEmpty();  // 搜索失败
            if(!fail){
                // 只获取部分
                int endIndex = start + lineNumber;
                if (endIndex > postList.size()){
                    endIndex = postList.size();
                }
                List<PostFront> posts = postList.subList(start, endIndex);
                PostsAndAuthors postsAndAuthors = convertService.findAuthors(posts);
                postsAndAuthors.setTotalPostNumber((long) postList.size());
                postsAndAuthors.setWords(words);
                return result.resultMap(true, "搜索成功", postsAndAuthors);
            }

            return result.resultMap(false, "搜索失败，未搜索到相关结果", null);
        }else {
            System.out.println("搜索用户");
            List<Long> uidList = new ArrayList<>();
            List<User> userList = new ArrayList<>();
            for (String word: words){
                List<User> users = userService.findUsersByName(word);
                for (User user: users){
                    if (!uidList.contains(user.getId())) {
                        uidList.add(user.getId());
                        userList.add(user);
                    }
                }
            }

            boolean fail = userList.isEmpty();
            if (!fail){
                return result.resultMap(true, "搜索成功", userList);
            }
            return result.resultMap(false, "未搜索到相关内容", null);
        }
    }
}
